<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/icon.css">
<link rel="stylesheet" href="static/css/style.css" />
<link rel="stylesheet" href="static/css/tcommon.css" />
<script src="static/jquery/jquery-1.12.4.js"></script>
<script src="static/easyui/jquery.easyui.min.js"></script>
<script src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>物资明细</title>
<style type="text/css">
.info_dialog .content_container .content_table tr td input {
	width: auto;
}
</style>
</head>
<body>
	<div class="content-container" data-options="region:'center'">
		<div class="panel-header">
			<div class="panel-title">物资明细列表</div>
		</div>
		<table id="pro_table" class="easyui-datagrid" style="height: 100%">
		</table>

		<div id="pro_tb">
			<div>
				日期：
				<input id="fromDate" class="easyui-datebox" editable="false" />
				~
				<input id="toDate" class="easyui-datebox" editable="false" />
				<a href="#" class="easyui-linkbutton" iconCls="icon-search" id="search">搜索</a>
			</div>
			<div>
				<a href="javascript:void(0)" class="d-btn btn-blue" onclick="newpro()">添加</a>
			</div>

		</div>
	</div>
	<div id="proDlg" class="easyui-dialog info_dialog"
		style="width: 800px; padding: 10px 20px; overflow-y: auto; top: 30px;" closed="true"
		buttons="#pro-dlg-buttons">
		<div class="title"></div>
		<form id="proFm" method="post">
			<div class="content_container">
				<table class="content_table">
					<tr>
						<td style="width: 100px;">类别：</td>
						<td>
							<input type="checkbox" name="type" value="1" />
							增加
							<input type="checkbox" name="type" value="2" />
							减少
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">数量：</td>
						<td colspan="2">
							<input class="easyui-textbox" data-options="required:true" id="difference" name="difference"
								style="width: 200px" />
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div id="pro-dlg-buttons">
		<a href="# " class="easyui-linkbutton " onclick="savePro()">保存</a>
		<a href="# " class="easyui-linkbutton btn-cancel " onclick="javascript:$('#proDlg').dialog('close')">关闭</a>
	</div>
</body>
<script th:inline="javascript">
	var currentPro;
	var fromDate = ""; 
	var toDate = ""; 
	var goods =[[${goods}]];
	
	$(document).ready(function() {
		fromDate = getLastMonth().last;
		toDate = getLastMonth().now;
		$("#fromDate").datebox('setValue', fromDate);
		$("#toDate").datebox('setValue', toDate);
		$('#pro_table').datagrid({
			rownumbers : true,
			singleSelect : true,
			pagination : true,
			fitColumns : false,
			border : false,
			striped : true,
            pageSize: 10, 
			toolbar : '#pro_tb',
			url : "goodscurrents",
			method : 'get',
			queryParams: {
				goodsid : goods.id,
				startdate : fromDate,
				enddate : toDate
			},
			onSelect : function(index, row) {
				currentPro = row;
			},
			columns : [ [ {
				field : 'name',
				title : '名称',
				width : '',
				formatter : function(value, row, index) {
					if(row.goods){
						return row.goods.name
					}
				}
			},{
				field : 'type',
				title : '类别',
				width : '',
				formatter : function(value, row, index) {
					if(row.type==1){
						return "加"
					}else{
						return "减"
					}
				}
			},{
				field : 'difference',
				title : '数量',
				width : ''
			},{
				field : 'createdate',
				title : '日期',
				width : '',
				formatter : formatDate
			},{
				field : 'status',
				title : '状态',
				width : '',
				formatter : function(value,row,index) {
					if (value == 4){
						return value = '已审';
					}else if (value == 3){
						return value = '已拒绝';
					}else if (value == 6){
						return value = '待二级审核';
					}
				}
			},{
				field : 'createrid',
				title : '执行人',
				width : '',
				formatter : function(value, row, index) {
					if(row.userinfo){
						return row.userinfo.name
					}
				}
			} ] ]
		});
	});
	
	$("input[name='type']:checkbox").click(function () {
        var flag = $(this).is(':checked');
        if (flag) {
            $(this).siblings("input").attr("checked", false);
        }
    });

	function refreshProTable() {
		$('#pro_table').datagrid('load',{
			goodsid : goods.id,
			startdate : fromDate,
			enddate : toDate
		});
	}
	
	$("#search").click(function(){
		fromDate = $("#fromDate").datebox('getValue');
		toDate = $("#toDate").datebox('getValue');
		refreshProTable();
	});

	function newpro() {
		$("#proDlg").dialog('open').dialog('setTitle', '添加物资明细');
		$("#proFm").form('clear');
		currentPro = {};
	}
	
	function savePro() {
		if (!$("#proFm").form('validate'))
			return;
		var type = $('input:checkbox[name="type"]:checked').val();
		currentPro.type = type;
		currentPro.difference = $("#difference").textbox('getValue');
		currentPro.goodsid = goods.id;
		$.ajax({
			type : 'POST',
			url : "goodsCurrentSave.do",
			data : JSON.stringify(currentPro),
			contentType : "application/json",
			success : function(data) {
				if (data.success) {
					$.messager.show({
						title : '保存物资明细',
						msg : '保存物资明细成功',
						timeout : 5000,
						showType : 'slide'
					});
					refreshProTable();
					window.parent.document.getElementById("goods").contentWindow.refreshProTable();
				} else {
					$.messager.alert('保存物资明细', '保存物资明细失败', 'error');
				}
				$('#proDlg').dialog('close');
			},
			dataType : "json"
		});
	}
	function formatDate(time) {
		var date = new Date(time);
		var year = date.getFullYear(), month = date.getMonth() + 1, //月份是从0开始的
		day = date.getDate(), hour = date.getHours(), min = date.getMinutes(), sec = date
				.getSeconds();
		var newTime = year + '-' + month + '-' + day;
		return newTime;
	}
	function getLastMonth() {
		var now = new Date();
		var year = now.getFullYear();
		var month = now.getMonth() + 1;//0-11表示1-12月
		var day = now.getDate() + 1;
		var dateObj = {};
		dateObj.now = year + '-' + month + '-' + day;
		var nowMonthDay = new Date(year, month, 0).getDate(); //当前月的总天数
		if (month - 1 <= 0){ //如果是1月，年数往前推一年<br>　　　　 
			dateObj.last = (year - 1) + '-' + 12 + '-' + day;
		}else{
			var lastMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();
			if (lastMonthDay < day){ //1个月前所在月的总天数小于现在的天日期
				if (day < nowMonthDay){ //当前天日期小于当前月总天数
					dateObj.last = year + '-' + (month - 1) + '-' + (lastMonthDay - (nowMonthDay - day));
				}else{
					dateObj.last = year + '-' + (month - 1) + '-' + lastMonthDay;
				}
			}else{
				dateObj.last = year + '-' + (month - 1) + '-' + day;
			}
		}
		return dateObj;
	}
	
	$.extend($.fn.textbox.defaults.rules, {
	     number: {//value值为文本框中的值
	           validator: function (value) {
	          var reg = /^\d+(\.\d+)?$/;
	           return reg.test(value);
	       },
        message : "请输入数字"
    },
    chinese : {
        validator : function(value, param) {
            var reg = /^[\u4e00-\u9fa5]+$/i;
            return reg.test(value);
        },
        message : "请输入中文"
    },
    checkLength: {
        validator: function(value, param){
            return param[0] >= get_length(value);
        },
        message: '请输入最大{0}位字符'
    },
    specialCharacter: {
        validator: function(value, param){
            var reg = new RegExp("[`~!@#$^&*()=|{}':;'\\[\\]<>~！@#￥……&*（）——|{}【】‘；：”“'、？]");
            return !reg.test(value);
        },
        message: '不允许输入特殊字符'
    },
    percent:{
    	validator: function (value, param) {
    		var percent = $("#remainpercent").text();
            return parseFloat(value)<=parseFloat(percent);
 
        },
        message: '不能超过剩余形象进度'
    },
    planexist:{
    	validator : function(value, param) {
	    	var data = {};
	    	if(currentPro.id){
	    		data.planid = currentPro.id;
	    	}else{
	    		data.planid= 0;
	    	}
	    	data.plandate = value;
	    	data.mainid = planmain.id;
    		var res = false;
    		$.ajax({
				type: 'POST',
				async : false,
				url: 'check/plandate',
				data: data,
				success: function(data) {
					res= data.success;
				},
				dataType: "json"
			});
			return res;
        },
        message : '已存在物资明细'
    }
    });
</script>
</html>